bitkeeper revision 1.1726.2.3 (42ba7587nwhnhQJ_hDkHTk8kTKtAFw)
authorkaf24@firebug.cl.cam.ac.uk <kaf24@firebug.cl.cam.ac.uk>
Thu, 23 Jun 2005 08:40:39 +0000 (08:40 +0000)
committerkaf24@firebug.cl.cam.ac.uk <kaf24@firebug.cl.cam.ac.uk>
Thu, 23 Jun 2005 08:40:39 +0000 (08:40 +0000)
Make sure we save errno across error-path printfs and munlocks. Based
on a patch from Anthony Liguori.
Signed-off-by: Keir Fraser <keir@xensource.com>
tools/libxc/xc_domain.c
tools/libxc/xc_evtchn.c
tools/libxc/xc_gnttab.c
tools/libxc/xc_misc.c
tools/libxc/xc_private.c
tools/libxc/xc_private.h

index 2edf11c39d71b3197963c9827c547e5b1d5df5f5..5ea0a33903c741bfea0b35be2ba02bc05de4714c 100644 (file)
@@ -128,7 +128,7 @@ int xc_domain_get_vcpu_context(int xc_handle,
                                u32 vcpu,
                                vcpu_guest_context_t *ctxt)
 {
-    int rc, errno_saved;
+    int rc;
     dom0_op_t op;
 
     op.cmd = DOM0_GETVCPUCONTEXT;
@@ -143,11 +143,7 @@ int xc_domain_get_vcpu_context(int xc_handle,
     rc = do_dom0_op(xc_handle, &op);
 
     if ( ctxt != NULL )
-    {
-        errno_saved = errno;
-        (void)munlock(ctxt, sizeof(*ctxt));
-        errno = errno_saved;
-    }
+        safe_munlock(ctxt, sizeof(*ctxt));
 
     if ( rc > 0 )
         return -ESRCH;
index 1c0294d83b10d4ea253e487cdc751a453a77e7fd..74e8468dd54749a1b5a1daeba5bf27207010a46c 100644 (file)
@@ -26,7 +26,7 @@ static int do_evtchn_op(int xc_handle, evtchn_op_t *op)
     if ((ret = do_xen_hypercall(xc_handle, &hypercall)) < 0)
         ERROR("do_evtchn_op: HYPERVISOR_event_channel_op failed: %d", ret);
 
-    (void)munlock(op, sizeof(*op));
+    safe_munlock(op, sizeof(*op));
  out:
     return ret;
 }
index ad23e68013e617958d161949f7d0b83ce4102b24..409539cb14074a633ca472894b6717a380db5e0d 100644 (file)
@@ -33,7 +33,7 @@ do_gnttab_op( int xc_handle,
     if ( (ret = do_xen_hypercall(xc_handle, &hypercall)) < 0 )
         ERROR("do_gnttab_op: HYPERVISOR_grant_table_op failed: %d", ret);
 
-    (void)munlock(op, sizeof(*op));
+    safe_munlock(op, sizeof(*op));
  out:
     return ret;
 }
index 40291bc3ef29f54d8471d390b07ecef6bb94ee40..ac306b1585f97d3e784551c0219c158d398ff146 100644 (file)
@@ -43,7 +43,7 @@ int xc_readconsolering(int xc_handle,
         *pnr_chars = op.u.readconsole.count;
     }
 
-    (void)munlock(buffer, nr_chars);
+    safe_munlock(buffer, nr_chars);
 
     return ret;
 }    
index 87e5ecd1f32b33ec6a7c76436a3888ae8bb68af6..fe8f42c1c81476d7de43a8dea9377ae2d50a1cdb 100644 (file)
@@ -22,8 +22,10 @@ void *xc_map_foreign_batch(int xc_handle, u32 dom, int prot,
     ioctlx.arr=arr;
     if ( ioctl( xc_handle, IOCTL_PRIVCMD_MMAPBATCH, &ioctlx ) < 0 )
     {
+        int saved_errno = errno;
        perror("XXXXXXXX");
-       munmap(addr, num*PAGE_SIZE);
+       (void)munmap(addr, num*PAGE_SIZE);
+        errno = saved_errno;
        return NULL;
     }
     return addr;
@@ -51,7 +53,9 @@ void *xc_map_foreign_range(int xc_handle, u32 dom,
     entry.npages=(size+PAGE_SIZE-1)>>PAGE_SHIFT;
     if ( ioctl( xc_handle, IOCTL_PRIVCMD_MMAP, &ioctlx ) < 0 )
     {
-       munmap(addr, size);
+        int saved_errno = errno;
+       (void)munmap(addr, size);
+        errno = saved_errno;
        return NULL;
     }
     return addr;
@@ -134,8 +138,8 @@ static int flush_mmu_updates(int xc_handle, mmu_t *mmu)
     }
 
     mmu->idx = 0;
-    
-    (void)munlock(mmu->updates, sizeof(mmu->updates));
+
+    safe_munlock(mmu->updates, sizeof(mmu->updates));
 
  out:
     return err;
@@ -232,7 +236,7 @@ int xc_get_pfn_list(int xc_handle,
 
     ret = do_dom0_op(xc_handle, &op);
 
-    (void)munlock(pfn_buf, max_pfns * sizeof(unsigned long));
+    safe_munlock(pfn_buf, max_pfns * sizeof(unsigned long));
 
 #if 0
 #ifdef DEBUG
index baf1e5f26d1743349e39f75b8c724dde63be154b..c50813ee3c76a54249ce5670e180bf02fd4bb482 100644 (file)
@@ -101,12 +101,28 @@ struct load_funcs
     loadimagefunc loadimage;
 };
 
-#define ERROR(_m, _a...)  \
-    fprintf(stderr, "ERROR: " _m "\n" , ## _a )
-
-#define PERROR(_m, _a...) \
-    fprintf(stderr, "ERROR: " _m " (%d = %s)\n" , ## _a , \
-            errno, strerror(errno))
+#define ERROR(_m, _a...)                                \
+do {                                                    \
+    int __saved_errno = errno;                          \
+    fprintf(stderr, "ERROR: " _m "\n" , ## _a );        \
+    errno = __saved_errno;                              \
+} while (0)
+
+
+#define PERROR(_m, _a...)                                       \
+do {                                                            \
+    int __saved_errno = errno;                                  \
+    fprintf(stderr, "ERROR: " _m " (%d = %s)\n" , ## _a ,       \
+            __saved_errno, strerror(__saved_errno));            \
+    errno = __saved_errno;                                      \
+} while (0)
+
+static inline void safe_munlock(const void *addr, size_t len)
+{
+    int saved_errno = errno;
+    (void)munlock(addr, len);
+    errno = saved_errno;
+}
 
 static inline int do_privcmd(int xc_handle,
                              unsigned int cmd, 
@@ -125,7 +141,7 @@ static inline int do_xen_hypercall(int xc_handle,
 
 static inline int do_dom0_op(int xc_handle, dom0_op_t *op)
 {
-    int ret = -1, errno_saved;
+    int ret = -1;
     privcmd_hypercall_t hypercall;
 
     op->interface_version = DOM0_INTERFACE_VERSION;
@@ -146,9 +162,7 @@ static inline int do_dom0_op(int xc_handle, dom0_op_t *op)
                     " rebuild the user-space tool set?\n");
     }
 
-    errno_saved = errno;
-    (void)munlock(op, sizeof(*op));
-    errno = errno_saved;
+    safe_munlock(op, sizeof(*op));
 
  out1:
     return ret;
@@ -163,7 +177,6 @@ static inline int do_dom_mem_op(int            xc_handle,
 {
     privcmd_hypercall_t hypercall;
     long ret = -EINVAL;
-    int errno_saved;
 
     hypercall.op     = __HYPERVISOR_dom_mem_op;
     hypercall.arg[0] = (unsigned long)memop;
@@ -186,11 +199,7 @@ static inline int do_dom_mem_op(int            xc_handle,
     }
 
     if ( extent_list != NULL )
-    {
-        errno_saved = errno;
-        (void)munlock(extent_list, nr_extents*sizeof(unsigned long));
-        errno = errno_saved;
-    }
+        safe_munlock(extent_list, nr_extents*sizeof(unsigned long));
 
  out1:
     return ret;
@@ -204,7 +213,6 @@ static inline int do_mmuext_op(
 {
     privcmd_hypercall_t hypercall;
     long ret = -EINVAL;
-    int errno_saved;
 
     hypercall.op     = __HYPERVISOR_mmuext_op;
     hypercall.arg[0] = (unsigned long)op;
@@ -224,9 +232,7 @@ static inline int do_mmuext_op(
                     " rebuild the user-space tool set?\n",ret,errno);
     }
 
-    errno_saved = errno;
-    (void)munlock(op, nr_ops*sizeof(*op));
-    errno = errno_saved;
+    safe_munlock(op, nr_ops*sizeof(*op));
 
  out1:
     return ret;